package com.leyou.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.threadlocals.UserHolder;
import com.leyou.common.utils.BeanHelper;
import com.leyou.user.dto.UserAddressDTO;
import com.leyou.user.entity.TbUserAddress;
import com.leyou.user.mapper.TbUserAddressMapper;
import com.leyou.user.service.TbUserAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户收货地址表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2020-10-07
 */
@Service
public class TbUserAddressServiceImpl extends ServiceImpl<TbUserAddressMapper, TbUserAddress> implements TbUserAddressService {
    @Override
    public List<UserAddressDTO> findAddressList() {
        Long userId = UserHolder.getUserId();
        QueryWrapper<TbUserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbUserAddress::getUserId,userId);
        List<TbUserAddress> tbUserAddressList = this.list(queryWrapper);
        return BeanHelper.copyWithCollection(tbUserAddressList,UserAddressDTO.class);
    }

    @Override
    public UserAddressDTO findById(Long id) {
        Long userId = UserHolder.getUserId();
        QueryWrapper<TbUserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbUserAddress::getUserId,userId);
        queryWrapper.lambda().eq(TbUserAddress::getId,id);
        TbUserAddress tbUserAddress = this.getOne(queryWrapper);
        return BeanHelper.copyProperties(tbUserAddress,UserAddressDTO.class);
    }
    /**
     * 设置默认收货地址
     * @param addressId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setDefaultAddress(Long addressId) {
        QueryWrapper<TbUserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbUserAddress::getIsDefault,1);
        int count = this.count(queryWrapper);
        if(count >0){
            UpdateWrapper<TbUserAddress> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().
                    eq(TbUserAddress::getIsDefault,1).
                    set(TbUserAddress::getIsDefault,0);
            boolean b = this.update(updateWrapper);
            if(!b){
                throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
            }
        }
        TbUserAddress tbUserAddress = new TbUserAddress().setIsDefault(1).setId(addressId);
        boolean b1 = this.updateById(tbUserAddress);
        if(!b1){
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    @Override
    public void delete(Long addressId) {
        Long userId = UserHolder.getUserId();
        QueryWrapper<TbUserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbUserAddress::getUserId,userId).eq(TbUserAddress::getId,addressId);
        boolean b = this.remove(queryWrapper);
        if(!b){
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

    @Override
    public UserAddressDTO findByIdAndUserId(Long addressId, Long userId) {
        QueryWrapper<TbUserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbUserAddress::getUserId,userId);
        queryWrapper.lambda().eq(TbUserAddress::getId,addressId);
        TbUserAddress tbUserAddress = this.getOne(queryWrapper);
        return BeanHelper.copyProperties(tbUserAddress,UserAddressDTO.class);
    }
}
